<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="商品标识" prop="goodId">
        <el-input v-model="dataForm.goodId" placeholder="商品标识"></el-input>
      </el-form-item>
      <el-form-item label="商品数量" prop="goodsNumber">
        <el-input v-model="dataForm.goodsNumber" placeholder="商品数量"></el-input>
      </el-form-item>
      <el-form-item label="售出数量" prop="saleNumber">
        <el-input v-model="dataForm.saleNumber" placeholder="售出数量"></el-input>
      </el-form-item>
      <el-form-item label="剩余数量" prop="remainNumber">
        <el-input v-model="dataForm.remainNumber" placeholder="剩余数量"></el-input>
      </el-form-item>
      <el-form-item label="存放地点" prop="storePlace">
        <el-input v-model="dataForm.storePlace" placeholder="存放地点"></el-input>
      </el-form-item>
      <el-form-item label="冻结数量" prop="freezeNumber">
        <el-input v-model="dataForm.freezeNumber" placeholder="冻结数量"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createUtc">
        <el-input v-model="dataForm.createUtc" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="modifyUtc">
        <el-input v-model="dataForm.modifyUtc" placeholder="更新时间"></el-input>
      </el-form-item>
      <el-form-item label="是否删除(数字0：未插入，1.未删除,2：删除，)" prop="isDeleted">
        <el-input v-model="dataForm.isDeleted" placeholder="是否删除(数字0：未插入，1.未删除,2：删除，)"></el-input>
      </el-form-item>
      <el-form-item label="版本号(数字0：未插入)" prop="version">
        <el-input v-model="dataForm.version" placeholder="版本号(数字0：未插入)"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          goodId: '',
          goodsNumber: '',
          saleNumber: '',
          remainNumber: '',
          storePlace: '',
          freezeNumber: '',
          createUtc: '',
          modifyUtc: '',
          isDeleted: '',
          version: ''
        },
        dataRule: {
          goodId: [
            { required: true, message: '商品标识不能为空', trigger: 'blur' }
          ],
          goodsNumber: [
            { required: true, message: '商品数量不能为空', trigger: 'blur' }
          ],
          saleNumber: [
            { required: true, message: '售出数量不能为空', trigger: 'blur' }
          ],
          remainNumber: [
            { required: true, message: '剩余数量不能为空', trigger: 'blur' }
          ],
          storePlace: [
            { required: true, message: '存放地点不能为空', trigger: 'blur' }
          ],
          freezeNumber: [
            { required: true, message: '冻结数量不能为空', trigger: 'blur' }
          ],
          createUtc: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          modifyUtc: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除(数字0：未插入，1.未删除,2：删除，)不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号(数字0：未插入)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.upkrepertory.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodId = data.upkrepertory.goodId
                this.dataForm.goodsNumber = data.upkrepertory.goodsNumber
                this.dataForm.saleNumber = data.upkrepertory.saleNumber
                this.dataForm.remainNumber = data.upkrepertory.remainNumber
                this.dataForm.storePlace = data.upkrepertory.storePlace
                this.dataForm.freezeNumber = data.upkrepertory.freezeNumber
                this.dataForm.createUtc = data.upkrepertory.createUtc
                this.dataForm.modifyUtc = data.upkrepertory.modifyUtc
                this.dataForm.isDeleted = data.upkrepertory.isDeleted
                this.dataForm.version = data.upkrepertory.version
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'goodId': this.dataForm.goodId,
              'goodsNumber': this.dataForm.goodsNumber,
              'saleNumber': this.dataForm.saleNumber,
              'remainNumber': this.dataForm.remainNumber,
              'storePlace': this.dataForm.storePlace,
              'freezeNumber': this.dataForm.freezeNumber,
              'createUtc': this.dataForm.createUtc,
              'modifyUtc': this.dataForm.modifyUtc,
              'isDeleted': this.dataForm.isDeleted,
              'version': this.dataForm.version
            }
            var tick = !this.dataForm.id ? API.upkrepertory.add(params) : API.upkrepertory.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
